<template>
  <div class="list-wrapper" ref="wrapper">
    <div>
      <div class="list-box">
        <label class="list-box__title">您的位置</label>
        <div class="list-box__content border-topbottom">
          <div class="button-list">
            <div class="button-item-wrapper">
            <div class="button-item active">{{this.currentCity}}</div>
          </div>
          </div>
        </div>
      </div>
      <div class="list-box">
        <label class="list-box__title">热门城市</label>
        <div class="list-box__content border-topbottom">
          <div class="button-list">
            <div class="button-item-wrapper" v-for="item of hot" :key="item.id">
              <div class="button-item" @click="handleCityClick(item.name)">{{item.name}}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="list-box" v-for="(item, key) of cities" :key="key" :ref="key">
        <label class="list-box__title">{{key}}</label>
        <div class="list-box__content border-topbottom">
          <ul class="alphabet">
            <li class="alphabet__item border-bottom" @click="handleCityClick(innerItem.name)" v-for="innerItem of item" :key="innerItem.id">{{innerItem.name}}</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String,
    lastCity: ''
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper, {
      click: true
    })
  },
  methods: {
    handleCityClick (city) {
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        // console.log(this.letter)
        if (this.letter) {
          const element = this.$refs[this.letter][0]
          this.scroll.scrollToElement(element, 200)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  @import "~styles/scss/variable.scss";
  @import "~styles/scss/mixins.scss";
  .list-wrapper{
    position: absolute;
    top: 1.7rem;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: hidden;
    .list-box {
      &__title {
        height: .6rem;
        line-height: .6rem;
        padding: 0 .2rem;
        color: $color-text-regular;
      }
      &__content {
        background: #fff;
        .button-list {
          padding: .1rem .6rem .1rem .2rem;
          overflow: hidden;
          .button-item-wrapper {
            box-sizing: border-box;
            float: left;
            width: 33.3%;
            padding: .1rem;
            .button-item {
              box-sizing: border-box;
              width: 100%;
              border: solid 1px #ddd;
              border-radius: 2px;
              padding: .14rem .2rem;
              color: $color-text-regular;
              font-size: $fontSize-small;
              text-align: center;
              @include ellipsis();
              &.active {
                border-color: $color-primary;
                color: $color-primary;
              }
            }
          }
        }
        .alphabet {
          overflow: hidden;
          &__item {
            font-size: $fontSize-regular;
            color: $color-text-regular;
            padding: .22rem .2rem;
            &:active {
              background: #eee;
            }
          }
        }
      }
    }
  }
</style>
